#include "Temperature.hpp"
#include <chrono>
#include <dds/dds.hpp>
#include <iostream>
#include <random>
#include <thread>
#include <vector>

using namespace SensorData;

int main(int argc, char *argv[])
{
    dds::domain::DomainParticipant    dp(0);
    dds::topic::Topic<Temperature>    topic(dp, "TemperatureTopic");
    dds::pub::Publisher               pub(dp);
    dds::pub::DataWriter<Temperature> dw(pub, topic);

    std::random_device                     rd;
    std::mt19937                           gen(rd());
    std::uniform_real_distribution<double> dis(20.0, 40.0);

    /* ---------- 1. 读取用户指定的序号 ---------- */
    std::vector<int32_t> ids;
    if (argc > 1)
    {
        ids.reserve(static_cast<size_t>(argc - 1));
        for (int i = 1; i < argc; ++i)
        {
            try
            {
                ids.push_back(std::stoi(argv[i]));
            }
            catch (...)
            {
                std::cerr << "非法整数: " << argv[i] << "，已跳过\n";
            }
        }
    }
    else
    {
        /* 默认还是 1 2 3 */
        ids = {1, 2, 3};
    }

    /* ---------- 2. 主循环 ---------- */
    Temperature temp;
    temp.unit("Celsius");

    while (true)
    {
        for (int32_t id : ids)
        {
            temp.sensor_id(id); // IDL 里仍是 int32_t
            temp.value(dis(gen));
            dw.write(temp);
            std::cout << "[sensor-" << id << "] >> temp = " << temp.value() << " ℃" << std::endl;
        }
        std::this_thread::sleep_for(std::chrono::seconds(2));
    }
    return 0;
}